package tp.view;

import tp.model.Bola;
import tp.model.SpriteCache;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Observable;
import java.util.Observer;
import tp.controller.*;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;

/**
 * Esta es la clase que representa a la vista del misil
 * 
 * @author Dr Von Nostrum
 * 
 */
public class BolaView extends Component implements Observer {

	private Graphics g;

	public Bola bola;

	public BufferedImage bolaImage;

	public BufferedImage bolaFuegoImage;

	public BolaView(Bola unaBola) {
		unaBola.addObserver(this);
		bola = unaBola;
		g = this.getGraphics();
	}

	private void checkOffscreenImage() {
		Dimension d = CopyOfBoardView1.panel.getSize();// Dimension d =
														// getSize();
		InputStream in = getClass().getResourceAsStream("bola.jpg");
		InputStream in1 = getClass().getResourceAsStream("bolafuego.jpg");
		JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
		JPEGImageDecoder decoder1 = JPEGCodec.createJPEGDecoder(in1);
		try {
			bolaImage = decoder.decodeAsBufferedImage();
			bolaFuegoImage = decoder1.decodeAsBufferedImage();
			in.close();
			in1.close();
		} catch (Exception ex) {

		}

	}

	public void paint(Graphics g) {
		if ((bola != null) && (bola.getMiVista() != null)) {
			Dimension d = CopyOfBoardView1.panel.getSize();// Dimension d =
															// this.getParent().getSize();
			checkOffscreenImage();
			// Graphics offG = misilImage.getGraphics();
			// offG.setColor(Color.GRAY);// getBackground());
			// offG.drawImage(misilImage, 0, 0, d.width, d.height, null);
			// offG.fillRect(0, 0, d.width, d.height);

			// Dibujo la imagen propieamente dicha.
			// paintOffscreen(bolaImage.getGraphics());

			// Renderizo la imagen finalmente en la pantalla.
			if ((bola != null) && (bola.isVivo() == true)) {
				if (bola.getClase() == "Bola")
					g.drawImage(bolaImage, bola.getMiPosicion().getXInt()
							- (int) bola.getMiRadio(), d.height
							- (bola.getMiPosicion().getYInt() + (int) bola
									.getMiRadio()),
							(int) bola.getMiRadio() * 2, (int) bola
									.getMiRadio() * 2, null);
				// g.drawImage(bolaImage, bola.getMiPosicion().getXInt() - (int)
				// bola.getMiRadio(), d.height - (bola.getMiPosicion().getYInt()
				// + (int) bola.getMiRadio()) , (int) bola.getMiRadio() * 2 ,
				// (int) bola.getMiRadio() * 2, null);
				else
					g.drawImage(bolaFuegoImage, bola.getMiPosicion().getXInt()
							- (int) bola.getMiRadio(), d.height
							- (bola.getMiPosicion().getYInt() + (int) bola
									.getMiRadio()),
							(int) bola.getMiRadio() * 2, (int) bola
									.getMiRadio() * 2, null);
			}
		}

	}

	/*
	 * public void paintOffscreen(Graphics g) { Dimension d =
	 * CopyOfBoardView1.panel.getSize();//Dimension d =
	 * this.getParent().getSize(); if ((bola!=null)&&(bola.isVivo()==true)){
	 * g.fillOval(miBola.getMiPosicion().getXInt()-(int)miBola.getMiRadio(),d.height -
	 * miBola.getMiPosicion().getYInt()-(int)miBola.getMiRadio(),
	 * (int)miBola.getMiRadio()*2, (int)miBola.getMiRadio()*2);
	 * g.drawImage(bolaImage,bola.getMiPosicion().getXInt()-(int)bola.getMiRadio(),d.height -
	 * bola.getMiPosicion().getYInt()-(int)bola.getMiRadio(),
	 * (int)bola.getMiRadio()*2, (int)bola.getMiRadio()*2,null); } }
	 */public void update(Observable unMisil, Object arg1) {
	}

	public void Destruir() {
		this.bola = null; // se eliminan las referencias colgadas
	}
}